package com.huahua.graduateschoolspringboot.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.huahua.graduateschoolspringboot.entity.po.CityNatureGas;
import com.huahua.graduateschoolspringboot.entity.po.CityNatureGasDevice;
import com.huahua.graduateschoolspringboot.enums.DeviceTypeEnum;
import com.huahua.graduateschoolspringboot.service.CityNatureGasDeviceService;
import com.huahua.graduateschoolspringboot.service.CityNatureGasService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Iterator;

/**
 * @author LoveHuaHua
 * @date 2023年05月07日 10:44
 * @description believe in yourself
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class NatureGasDeviceCommandHandler implements IDeviceCommandHandler {

    private static final Integer BASE = 50;
    private final ObjectMapper objectMapper;
    private final CityNatureGasService cityNatureGasService;
    private final CityNatureGasDeviceService cityNatureGasDeviceService;

    /**
     * 是否支持
     *
     * @param deviceType 设备类型
     * @return boolean
     */
    @Override
    public boolean support(String deviceType) {
        return DeviceTypeEnum.NATURE_GAS.getPackageIdentification().equals(deviceType);
    }

    /**
     * 执行
     *
     * @param objectNode 对象节点
     */
    @Override
    public void execute(ObjectNode objectNode) throws JsonProcessingException {
        ObjectNode jsonNodes = objectMapper.readValue(objectNode.toString(), ObjectNode.class);
        Iterator<String> stringIterator = jsonNodes.fieldNames();
        stringIterator.forEachRemaining(roomName -> {
            Integer currentConcentration = jsonNodes.get(roomName).asInt();
            roomName = roomName.replace("room","房子");
            int currentStatus = currentConcentration <= BASE ? 0 : 1;
            CityNatureGasDevice one = cityNatureGasDeviceService.lambdaQuery().eq(CityNatureGasDevice::getRoomName, roomName).one();
            if (one == null) {
                one = new CityNatureGasDevice();
            }
            one.setCurrentConcentration(currentConcentration);
            one.setStatus(currentStatus);
            one.setRoomName(roomName);
            cityNatureGasDeviceService.saveOrUpdate(one);

            //查询纪录表最后一条纪录
            CityNatureGas natureGas = cityNatureGasService.lambdaQuery().eq(CityNatureGas::getRoomName, roomName).last("limit 1").one();
            if (currentStatus == 1) {
                //如果当前在报警
                if (natureGas.getStatus() == 1) {
                    //最新纪录也在报警 变更最大溶度
                    Integer overConcentration = Math.max(natureGas.getOverConcentration(), currentConcentration);
                    natureGas.setOverConcentration(overConcentration);
                    cityNatureGasService.updateById(natureGas);
                } else {
                    //最新纪录不在报警 新增报警纪录
                    CityNatureGas cityNatureGas = new CityNatureGas();
                    cityNatureGas.setRoomName(roomName);
                    cityNatureGas.setStatus(1);
                    cityNatureGas.setOverConcentration(currentConcentration);
                    cityNatureGasService.save(cityNatureGas);
                }
            } else {
                if (natureGas == null){
                    return;
                }
                //如果当前不在报警 但是数据库在报警
                if (natureGas.getStatus() == 1) {
                    //变更为不报警
                    natureGas.setStatus(0);
                    cityNatureGasService.updateById(natureGas);
                }

            }

        });

    }

}
